package com.aboverock.module.system.web;

import com.aboverock.common.domain.Datagrid;
import com.aboverock.common.domain.Result;
import com.aboverock.common.enums.LogTypeEnum;
import com.aboverock.common.helper.PageHelper;
import com.aboverock.core.spring.web.StringIdBaseController;
import com.aboverock.module.system.domain.Log;
import com.aboverock.module.system.service.LogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Rock Wang
 * @since 2018-10-31
 */
@RestController
@RequestMapping("system/log")
public class LogController extends StringIdBaseController<Log> {

    @Autowired
    private LogService logService;

    /**
     * 分页查询.
     *
     * @param request the request
     * @return 分页查询结果
     */
    @ApiOperation(value = "分页获取日志信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = CURRENT_PAGE_DEFAULT, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = PAGE_SIZE_DEFAULT, paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "sort", value = "排序，如createTime,desc;column1,column2,asc", paramType = "query"),
        @ApiImplicitParam(name = "type", value = "日志类型，0:登录日志，1：安全日志，2：异常日志，3：定时任务日志", paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "loginName", value = "用户登录名", paramType = "query") })
    @Override
    public Result page(HttpServletRequest request) {
        String type = request.getParameter("type"); 
        String loginName = request.getParameter("loginName");
        
        QueryWrapper<Log> query = new QueryWrapper<Log>();
        if (type != null) {
            query = query.eq("type", type);
        }
        if (!StringUtils.isBlank(loginName)) {
            query = query.likeRight("login_name", loginName);
        }

        Page<Log> page = PageHelper.page(Log.class);
        logService.page(page, query);
        
        if (page != null) {
            page.getRecords().forEach(log -> log.setTypeShow(LogTypeEnum.getLogType(log.getType()).getDescription()));
        }
        return Result.success(new Datagrid<Log>(page));
    }

    @ApiOperation(value = "获取日志类型")
    @GetMapping("type")
    public Result getLogType() {
        ObjectMapper mapper = new ObjectMapper();
        List<ObjectNode> logTypes = new ArrayList<>();
        for (LogTypeEnum logType : LogTypeEnum.values()) {
            ObjectNode node = mapper.createObjectNode();
            node.put("code", logType.getValue());
            node.put("description", logType.getDescription());
            logTypes.add(node);
        }
        return Result.success(logTypes);
    }

    @Override
    @ApiOperation(value = "禁用该通用接口")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result all(HttpServletRequest request) {
        return null;
    }

    @Override
    @ApiOperation(value = "禁用该通用接口")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result getById(String id) {
        return null;
    }

    @Override
    @ApiOperation(value = "禁用该通用接口")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result save(Log entity) {
        return null;
    }

    @Override
    @ApiOperation(value = "禁用该通用接口")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result update(String id, Log entity) {
        return null;
    }

    @Override
    @ApiOperation(value = "禁用该通用接口")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result delete(String id) {
        return null;
    }

}

